/**
 *
 * Copyright 2016-present reading
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import React from 'react';
import PropTypes from 'prop-types';
import { ViewPropTypes, Text, TouchableOpacity, View, StyleSheet, Platform } from 'react-native';
// import ViewOverflow from 'react-native-view-overflow';
import { connect } from 'react-redux';


const propTypes = {
  onPress: PropTypes.func,
  disabled: PropTypes.bool,
  style: Text.propTypes.style,
  containerStyle: ViewPropTypes.style,
  text: PropTypes.any,
  activeOpacity: PropTypes.number
};

const Badge = ({
  onPress,
  disabled,
  style,
  containerStyle,
  activeOpacity,
  children,
  state,
  msg,
  show
}) => (
    <View name="badgeContainer" style={styles.containerStyle}>
      { children }
      { show && !!msg.count &&
        <View name="badgeTextWrapper" style={styles.badgeTextWrapperStyle}>
          <View name="badgTextBody" style={styles.badgeStyle}>
            <Text name="badgTextContent" style={styles.badgeTextStyle}>{msg.count > 99 ? '99+' : msg.count}</Text>
          </View>
        </View>
      }
    </View>
);

Badge.propTypes = propTypes;

Badge.defaultProps = {
  onPress() {},
  disabled: false,
  activeOpacity: 0.8
};

const styles = StyleSheet.create({
  containerStyle: {
    flexDirection: 'row',
    alignItems: 'center',
    justifyContent: 'center',
    borderColor: 'red',
    overflow: 'visible',
    ...Platform.select({
      android: {
        paddingTop: 0,
        width: '100%',
        height: '100%',
        alignItems: 'flex-end',
        // backgroundColor: 'lightyellow',
      }
    })
  },
  badgeTextStyle: {
    color: 'white',
    fontSize: 12,
  },
  badgeTextWrapperStyle: {
    borderRadius: 20,
    alignSelf: 'flex-start',
    alignItems: 'flex-start',
    top: -2,
    left: 10,
    height: 18,
    flex: 1,
    zIndex: 10,
    position: 'absolute',
    ...Platform.select({
      android: {
        top: 2,
        left: 0,
        marginLeft: '50%'
      }
    })
  },

  badgeStyle: {
    flex: 1,
    borderRadius: 6,
    backgroundColor: 'red',
    padding: 1,
    paddingLeft: 4,
    paddingRight: 4
  }
});


const mapStateToProps = (state) => {
  const { msg } = state;
  return {
    msg
  };
};

const mapDispatchToProps = (dispatch) => {
  return {
  };
};

export default connect(mapStateToProps, mapDispatchToProps)(Badge);
